package puzzle.common.data;

import puzzle.common.point.Pnt;


public abstract class AbstractBoard<B extends AbstractBoard<B>> {

	public final int width;
	public final int height;

	protected AbstractBoard(int w, int h) {
		width = w;
		height = h;
	}
	
	/**
	 * 幅を論理的なマスの数を返す。
	 * @return
	 */
	public int getLogicalWidth() {
		return width;
	}
	
	/**
	 * 高さを論理的なマスの数を返す。
	 * @return
	 */
	public int getLogicalHeight() {
		return height;
	}

	protected AbstractBoard(B other) {
		width = other.width;
		height = other.height;
	}
	
	public abstract B dup();
	
	public final void copyFrom(B other) {
		assert width == other.width && height == other.height;
		copyFromImpl(other);
	}
	
	protected abstract void copyFromImpl(B other);
	
	public final boolean inRange(int x, int y) {
		return x >= 0 && x < width && y >= 0 && y < height;
	}
	
	public final boolean inRange(Pnt p) {
		return inRange(p.x, p.y);
	}
	
	public final int getLinearIndex(int x, int y) {
		return x + y * width;
	}
	
	public abstract void snapshotDirtyFlags();
	
}
